﻿/*
    Controls related static scripts.
*/

$(function () {
    $("#globalWelcome .sf-menu").superfish({
        delay: 500, autoArrows: false, dropShadows: false, speed: "fast"
    });

    $(window).resize(function () {
        Spectrum.Commons.adjustWindowSize();
    });

    Spectrum.Commons.adjustWindowSize();
});

var Spectrum = Spectrum || {};
Spectrum.Commons = Spectrum.Commons || {};

// Asks user to change current credentials and redirects back to the current or target page.
Spectrum.Commons.signInAsDifferentUser = function(bouncePage, targetPage) {
    var target = !!targetPage ? targetPage : window.location.href;

    window.location = bouncePage + "?signIn=true&uncache=" + (new Date().getTime()) + "&returnTo=" + encodeURIComponent(target);
}

Spectrum.Commons.adjustWindowSize = function () {
    // Keep body height to at least window size.
    $("body").css("minHeight", $(window).height());
}

// Used to track dialog boxes that can be closed from inside the iframe.
var colorboxShown = false;

// Initialize tagger.
$(function () {
    $(".tagger").hover(function () { $(".tagger").css({ "background-color": "#d2d2dd", "border-color": "#aaaabe" }); }, function () { $(".tagger").css({ "background-color": "transparent", "border-color": "transparent" }); });

    $(".tagger").click(function (e) {
        var baseUrl = insiteRootUrl + "/my/_layouts/SocialDataFrame.aspx";

        baseUrl += "?Title=" + encodeURIComponent(document.title);
        baseUrl += "&Src=bm";
        baseUrl += "&DisplayUrl=" + encodeURIComponent(window.location);
        baseUrl += "&Url=" + encodeURIComponent(window.location);

        $.colorbox({ href: baseUrl, iframe: true, innerWidth: 620, innerHeight: 420, transition: 'fade' });

        e.preventDefault();
        return false;
    });
});

// Initialize feedback.
$(function () {
    $(".feedback").hover(function () { $(".feedback").css({ "background-color": "#d2d2dd", "border-color": "#aaaabe" }); }, function () { $(".feedback").css({ "background-color": "transparent", "border-color": "transparent" }); });

    $(".feedback").click(function (e) {
        var baseUrl = insiteRootUrl + "/_layouts/Spectrum.InSite.Resources/Actions/SendFeedback.aspx";

        // Clip any hash fragments.
        var l = window.location.href;
        var hash = l.indexOf("#");

        if (hash >= 0)
            l = l.substr(0, hash);

        baseUrl += "?from=" + encodeURIComponent(l);
        baseUrl += "&IsFrame=1&IsDlg=1";

        // Uses iframe closing requests.
        colorboxShown = true;
        $.colorbox({ href: baseUrl, iframe: true, innerWidth: 620, innerHeight: 250, transition: 'fade' });

        e.preventDefault();
        return false;
    });
});

// Initialize dialog monitor that will close colorboxes when necessary by request from inside of iframe.
$(function () {
    setInterval(function () {
        if (colorboxShown && window.location.href.indexOf("#closeDialog") >= 0) {
            window.location = window.location.href.replace("#closeDialog", "#nop");

            colorboxShown = false;

            setTimeout(function () {
                parent.$.colorbox.close(); 
            }, 100);
        }
    }, 250);
});